define(function(require, exports){
	var $ = require("jquery");
	var pubjs = require('pubjs');
	var view = require('@base/view');
	var util = require('util');
	var dspUtil = require('dspUtil');

	var Base = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'or_count': 1,
				'class': 'P-CustomDownloadUser',
				'view_model': {
					view: {
						add: true
					}
				}
			});

			this.$data = [];
			this.Super('init', arguments);
		}
		,afterBuild:function(){
			this.append(`
				<div>
					<span class="" pub-name="dim_cond" pub-mod="report/module/dim.base"/>
					<div style="margin-top: -45px">
						<span ms-class="dn: !view.add" pub-name="add" pub-mod="@base/common/input.button"/>
						<div class="pt10" pub-name="grid" pub-mod="report/grid/condition.base"></div>
					</div>
				</div>
			`);

			this.buildFromTemplate({
				add: {
					'value': LANG('添加条件')
					,'class': 'btn btn-default'
					,'width': null
				}
			});

			this.createAsync('pop',"report/popwin/condition.base");
		}
		,onInputClick: function(ev) {
			var c = this.getConfig();
			switch (ev.name) {
				case 'add':
					this.showPop();
					break;
				default :
					return false;
			}
		}
		,showPop: function(data, index) {
			var self = this;
			var mod = self.$.pop;
			if(mod)
			{
				mod.reset().show();
				if(data)
				{
					mod.setData(data, index);
				}
			}
		}
		,checkUI: function()
		{
			this.vm.$.view.add = this.$data && this.$data.length < this.getConfig('or_count');
		}
		,onListOpClick:function(ev){
			//编辑单条记录
			var self = this;
			var c = self.getConfig();
			var index = ev.param.index;
			var data = ev.param.data;
			if(ev.param.op == 'edit')
			{
				self.showPop(data, index);
			}
			else if(ev.param.op == 'del')
			{
				pubjs.confirm(LANG('确认要删除此记录吗'), function(res)
				{
					if(res)
					{
						self.$data.splice(index, 1);
						self.$.grid.setData(self.$data);
						self.checkUI();
					}
				});
			}
			return false;
		}
		,onConditionSelect: function(ev)
		{
			var index = ev.param.index;
			var value = ev.param.value;
			if(util.isNumber(index))
			{
				this.$data[index] = value;
			}
			else
			{
				this.$data.push(value);
			}

			this.$.grid.setData(this.$data);
			this.checkUI();
		}
		,validate: function(data){
			var vm = this.vm.$;
			return true;
		}
		,getValue: function()
		{
			return {
				dim_cond: this.$.dim_cond.getData()
				,grid_cond: this.$data
			};
		}
		,onUserSet: function(ev)
		{
			var uids = ev.param && ev.param.ids || [];
			if(dspUtil.isDiff(uids, this.$userId || []))
			{
				if(this.$data && this.$data.length)
				{
					util.each(this.$data, function(item) {
						if(item)
						{
							util.each(item, function(t) {
								if(t.dimId == 'campaign_label')
								{
									return null;
								}
							});

							if(!item.length)
							{
								return null;
							}
						}
					});
				}

				this.$.grid.setData(this.$data);
				this.checkUI();
				this.$userId = uids;
			}
		}
		,setData: function(v)
		{
			this.$data = v.grid_cond || [];
			this.$.grid.setData(this.$data);
			this.$.dim_cond.setData(v.dim_cond);
			this.checkUI();
			return this;
		}
		,reset: function(){
			this.Super('reset', arguments);
			this.$.grid.setData([]);
			this.$data = [];
			this.vm.reset();
			return this;
		}
	});

	exports.base = Base;
});